﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Threading.Tasks;

namespace DigDes.DSchool.SUPS.DataAccess.Database
{
    public class CoachDAC : BaseDAC
    {
        public static void InsertRow(string sqlCommandText, SqlParameter[] sqlParametrs)
        {
            using (var sqlConn = new SqlConnection(BaseDAC.connectionString))
            using (var sqlCmd = sqlConn.CreateCommand())
            {
                sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                sqlCmd.CommandText = sqlCommandText;
                sqlCmd.Parameters.AddRange(sqlParametrs);
                sqlConn.Open();
                try
                {
                    sqlCmd.ExecuteNonQuery();
                }
                catch(Exception ex)
                {
                    Console.WriteLine("Невозможно добавить поле");
                    Console.WriteLine(ex.Message);             
                }
            }
        }

        

        public static IEnumerable<CoachInfoView> GetCoachInformation()
        {
            using (var sqlConn = new SqlConnection(connectionString))
            using (var sqlCmd = sqlConn.CreateCommand())
            {
                sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
                sqlCmd.CommandText = "GetCoachInformation";
                sqlConn.Open();

                using (SqlDataReader reader = sqlCmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        CoachInfoView row = new CoachInfoView();

                        row.Inventory_Number = ConvertFromDBDataToString(reader["Inventory_Number"]);
                        row.Cargo_Weight = ConvertFromDBDataToString(reader["Cargo_Weight"]);
                        row.Destination_Name = ConvertFromDBDataToString(reader["Destination_Name"]);
                        row.Cargo_Name = ConvertFromDBDataToString(reader["Cargo_Name"]);
                        row.Receiver_Code = ConvertFromDBDataToString(reader["ShipmentClient_Name"]);
                        row.Park_Type = ConvertFromDBDataToString(reader["Park_Type"]);
                        row.Start_Station_Name = ConvertFromDBDataToString(reader["Start_Station_Name"]);
                        row.Operation_Code = ConvertFromDBDataToString(reader["Operation_Name"]);
                        row.Operation_Data = ConvertFromDBDataToString(reader["Operation_Data"]);
                        row.Operation_Year = ConvertFromDBDataToString(reader["Operation_Year"]);
                        row.Operation_Time = ConvertFromDBDataToString(reader["Operation_Time"]);
                        row.Station_Of_Operation_Name = ConvertFromDBDataToString(reader["Station_Of_Operation_Name"]);
                        row.Delivery_Road_Name = ConvertFromDBDataToString(reader["Delivery_Road_Name"]);
                        row.Reception_Road_Name = ConvertFromDBDataToString(reader["Reception_Road_Name"]);
                        row.Coach_Index = ConvertFromDBDataToString(reader["Coach_Index"]);
                        row.Coach_Number = ConvertFromDBDataToString(reader["Coach_Number"]);

                        yield return row;
                    }
                }
            }
        }
    }
}
